package com.dhh.web.modules.menu.controller;

import com.dhh.common.utils.R;
import com.dhh.web.modules.AbstractController;
import com.dhh.web.modules.menu.domain.Menu;
import com.dhh.web.modules.menu.service.MenuService;
import com.dhh.web.modules.user.domain.Perms;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import static com.dhh.web.common.utils.UserConst.SYSTEM_MENU_DELETE;
import static com.dhh.web.common.utils.UserConst.SYSTEM_MENU_LIST;
import static com.dhh.web.common.utils.UserConst.SYSTEM_MENU_SAVE;

/**
 * @Auther: dhh
 * @Date: 2018/11/27 16:53
 * @Email: fzlinjianhui@163.com
 * @Description:
 */
@RestController
@RequestMapping("/menu/")
public class MenuController extends AbstractController {

    @Autowired
    private MenuService menuService;

    /**
     * 菜单树获取
     * @return R
     */
    @GetMapping("/tree")
    public R tree(){
        Set<Perms> perms = getPerms();
        List<Menu> menuList = menuService.findTopMenuByPerms(perms);
        return R.ok(new HashMap<String,Object>(){{put("data",menuList);}});
    }

    /**
     * 菜单列表（表格）
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions(value = SYSTEM_MENU_LIST)
    public R list(){
        List<Menu> allMenu = menuService.findAllMenu();
        return R.ok(allMenu);
    }

    /**
     * 保存菜单
     * @return R
     */
    @PostMapping("/save")
    @RequiresPermissions(value = SYSTEM_MENU_SAVE)
    public R save(Menu menu){
        menuService.save(menu);
        return R.ok();
    }

    /**
     * 删除菜单
     * @param menuId 菜单id
     * @return R
     */
    @PostMapping("/del")
    @RequiresPermissions(value = SYSTEM_MENU_DELETE)
    public R delete(String menuId){
        menuService.delete(menuId);
        return R.ok();
    }

}
